package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Game;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.view.RoundRectImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建者 zx
 * 创建时间 2021/2/3 15:25
 */
public class GameAdapter extends BaseRecyclerAdapter<GameAdapter.Holder, Game> {
    private final String TAG = "GameAdapter";


    public GameAdapter(Context context) {
        super(context);
    }

    @Override
    protected Holder getHolder(View v) {
        return new Holder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_game;
    }

    @Override
    public int getItemCount() {
        if (data == null) return 1;
        return data.size() + 1;
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (data == null || position == data.size()) {
            holder.tips_tv.setVisibility(View.VISIBLE);
            holder.content_layout.setVisibility(View.GONE);
            return;
        }
        holder.tips_tv.setVisibility(View.GONE);
        holder.content_layout.setVisibility(View.VISIBLE);
        final Game entity = data.get(position);
        Glide.with(context).load(entity.getIcon()).into(holder.gameIcon);
        holder.gameName.setText(entity.getGameName());
        holder.gameLabel.setText(entity.getGameType());
        holder.gameTotalReward.setText(new StringBuilder("总奖")
                .append(entity.getTotalMoney())
                .append("元"));
        holder.gamePeopleCount.setText(new StringBuilder("已参与")
                .append(entity.getParticipateCount())
                .append("人"));
        holder.gamePrice.setText(new StringBuilder("¥").append(entity.getBasicRewardMoney()));
        holder.gameBasicTask.setText(entity.getBasicTask());
        holder.gamePlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.onViewClick(v, "play", position, entity);
                }
            }
        });
    }


    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.game_icon)
        RoundRectImageView gameIcon;
        @BindView(R.id.game_name)
        TextView gameName;
        @BindView(R.id.game_label)
        TextView gameLabel;
        @BindView(R.id.game_total_reward)
        TextView gameTotalReward;
        @BindView(R.id.game_people_count)
        TextView gamePeopleCount;
        @BindView(R.id.game_price)
        TextView gamePrice;
        @BindView(R.id.game_basic_task)
        TextView gameBasicTask;
        @BindView(R.id.game_play)
        TextView gamePlay;
        @BindView(R.id.tips_tv)
        TextView tips_tv;
        @BindView(R.id.content_layout)
        View content_layout;

        public Holder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
